<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="申请处理" name="first">
        <el-table
          :data="applyList"
          border
          fit
          highlight-current-row>
          <el-table-column prop="applytime" label="申请时间" width="180"/>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="email" label="邮箱"/>
          <el-table-column label="状态" width="80" >
            <template slot-scope="scope">
              <el-tag v-show="scope.row.userStatue === 0" type="success">正常</el-tag>
              <el-tag v-show="scope.row.userStatue === 1" type="danger">被封禁</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="清洗次数" width="120" >
            <template slot-scope="scope">
              当前次数：{{ scope.row.cleannum }}<br>
              {{ scope.row.type===0?'申请次数：'+scope.row.num:'' }}
            </template>
          </el-table-column>
          <el-table-column label="分析次数" width="120" >
            <template slot-scope="scope">
              当前次数：{{ scope.row.analyzenum }}<br>
              {{ scope.row.type===1?'申请次数：'+scope.row.num:'' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="passNum(scope.row.id)">通过</el-button>
              <el-button type="danger" size="mini" @click="rejectNum(scope.row.id)">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          :current-page="applyPage.apage"
          :page-size="applyPage.alimit"
          :total="applyPage.atotal"
          :page-sizes="[5,10, 20, 50]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleListCurrent"
          @size-change="handleListSize"
        />
      </el-tab-pane>
      <el-tab-pane label="处理历史" name="second">
        <el-table
          :data="historyList"
          border
          fit
          highlight-current-row>
          <el-table-column prop="dealtime" label="处理时间" width="180"/>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="email" label="邮箱"/>
          <el-table-column label="状态" width="80" >
            <template slot-scope="scope">
              <el-tag v-show="scope.row.userStatue === 0" type="success">正常</el-tag>
              <el-tag v-show="scope.row.userStatue === 1" type="danger">被封禁</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="applytime" label="申请时间" width="160"/>
          <el-table-column label="申请详情" width="160" >
            <template slot-scope="scope">
              申请类型：{{ scope.row.type===0?'清理次数':'分析次数' }}<br>
              申请数量：{{ scope.row.num }}
            </template>
          </el-table-column>
          <el-table-column label="处理详情" width="80" >
            <template slot-scope="scope">
              <el-tag v-show="scope.row.state === 1" type="success">已通过</el-tag>
              <el-tag v-show="scope.row.state === -1" type="danger">已拒绝</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          :current-page="historyPage.hpage"
          :page-size="historyPage.hlimit"
          :total="historyPage.htotal"
          :page-sizes="[5,10, 20, 50]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import Apply from '@/api/apply.js'

export default{
  data() {
    return {
      applyList: null,
      applypage: {
        page: 1,
        limit: 5,
        total: 0
      },
      historyList: null,
      historyPage: {
        page: 1,
        limit: 5,
        total: 0
      },
      activeName: 'first'
    }
  },

  created() {
    this.applypage = {
      apage: 1,
      alimit: 5,
      atotal: 0
    }
    this.historyPage = {
      hpage: 1,
      hlimit: 5,
      htotal: 0
    }
    this.getList()
  },
  methods: {
    // 获得列表
    getList() {
      const page = this.applypage.apage
      const limit = this.applypage.alimit
      Apply.getNumList(page, limit)
        .then(response => {
          this.applyList = response.data.item
          this.applyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 获得处理过的记录
    getHistory() {
      const page = this.historyPage.hpage
      const limit = this.historyPage.hlimit
      Apply.getNumHistory(page, limit)
        .then(response => {
          this.historyList = response.data.item
          this.historyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 通过该申请
    passNum(id) {
      this.$confirm('通过该请求?', '确定', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Apply.applyNum(id)
          .then(response => {
            this.$message({
              message: '审核成功',
              type: 'success'
            })
            this.getList()
          })
      }).catch(() => {})
    },
    // 拒绝该申请
    rejectNum(id) {
      this.$confirm('拒绝该请求', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        Apply.rejectNum(id)
          .then(response => {
            this.$message({
              message: '审核成功',
              type: 'success'
            })
            this.getList()
          })
      }).catch(() => {})
    },
    // 分页
    handleListCurrent(val) {
      this.applyPage.apage = val
      this.getList()
    },
    handleListSize(val) {
      this.applyPage.alimit = val
      this.applyPage.apage = 1
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.historyPage.hpage = val
      this.getHistory()
    },
    handleSizeChange(val) {
      this.historyPage.hlimit = val
      this.historyPage.hpage = 1
      this.getHistory()
    },

    handleClick(val) {
      if (val.name  === 'first') {
        this.getList()
      } else {
        this.getHistory()
      }
    },
    applyPage() {
      return this.applyPage
    }
  }
}
</script>
